import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  SetMetadata,
  ParseIntPipe,
  Req,
  Query
} from '@nestjs/common';
import { MenuService } from './menu.service';

import { ReflectMetadataKeys } from '@/common/enum/ReflectMetadataKeys';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Menu } from './entities/menu.entity';
import { CreateMenuDto } from './dto/create-menu.dto';
import { UpdateMenuDto } from './dto/update-menu.dto';
import { menuQueryVo } from '@/common/interface';


@ApiTags('菜单管理')
@Controller('menu')
export class MenuController {
  constructor(private readonly menuService: MenuService) {}

  @ApiOperation({ summary: '菜单列表' })
  @ApiResponse({ status: 200, description: '200', type: [Menu] }) // 响应类型
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '菜单列表')
  @Get()
  findAll(
    @Query() params: menuQueryVo
  ) {
    return this.menuService.findMenuTree(params);
  }

  @ApiOperation({ summary: '创建菜单' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '创建菜单')
  @Post()
  create(@Body() createMenuDto: CreateMenuDto) {
    return this.menuService.create(createMenuDto);
  }

  @ApiOperation({ summary: '修改菜单' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '修改菜单')
  @Patch()
  update(@Body() updateMenuDto: UpdateMenuDto) {
    return this.menuService.update(updateMenuDto);
  }

  @ApiOperation({ summary: '删除菜单' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '删除菜单')
  @Delete(':id')
  remove(@Param('id', ParseIntPipe) id: number) {
    return this.menuService.remove(+id);
  }


  @ApiOperation({ summary: '权限菜单路由' })
  @Get('routers')
  findPermissionAll(@Req() req:any) {
    return this.menuService.findPermissionMenuTree(req?.user?.roles,req?.user?.isAdmin);
  }

  @ApiOperation({ summary: '菜单详细' })
  @ApiResponse({ status: 200, description: '200', type: Menu })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '菜单详细')
  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.menuService.findOne(+id);
  }

  @ApiOperation({ summary: '改变菜单的显示隐藏状态' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '改变菜单的显示隐藏状态')
  @Patch('display/:id/:display')
  changeDisplay(
    @Param('id', ParseIntPipe) id: number,
    @Param('display', ParseIntPipe) display: number,
  ) {
    return this.menuService.changeDisplay(id, display);
  }


  @ApiOperation({ summary: '设置是否为快捷菜单' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '设置是否为快捷菜单')
  @Patch('quickMenu/:id/:quickMenu')
  setQuickMenu(
    @Param('id', ParseIntPipe) id: number,
    @Param('quickMenu', ParseIntPipe) quickMenu: number,
  ) {
    return this.menuService.setQuickMenu(id, quickMenu);
  }

}
